import { reqGoodsInfo,reqAddOrUpdateShopCart } from "@/api"
import { Promise } from "core-js";
//封装临时游客身份
import {getUUID} from '@/utils/uuid_token'

const state={
    goodInfo:{},
    //游客临时身份
    uuid_token:getUUID()
}
const mutations={
    GETGOODINFO(state,goodInfo){
        state.goodInfo = goodInfo;
    },
}
const actions={
    //获取产品信息
   async getGoodInfo({commit},skuId){
     let result  = await reqGoodsInfo(skuId);
     if(result.code==200){
        commit("GETGOODINFO",result.data);
     }
    },
    //将产品添加到购物车||修改
   async addOrUpdateShopCart({commit},{skuId,skuNum}){
    //加入购物车返回的结果
        let result = await reqAddOrUpdateShopCart(skuId,skuNum)
        if(result.code==200){
            //返回成功
            return "ok"
        }else{
            //加入购物车失败
            return Promise.reject(new Error("faile"))
        }
    }
}
const getters={
    //简化导航信息
    categoryView(state){
        return state.goodInfo.categoryView||{}
    },
    //简化产品信息
    skuInfo(state){
        return state.goodInfo.skuInfo||{}
    },
    //产品的售卖属性
    spuSaleAttrList(){
        return state.goodInfo.spuSaleAttrList||[]
    }
}

export default{
    state,
    mutations,
    actions,
    getters,
}